package com.dungpt.truyenhay;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

/**
 * Created by TienDung on 12/15/2014.
 */
public class ListCategoryFragment extends Fragment {
    Context c;
    String[] categories = {"Tiên hiệp", "Kiếm hiệp", "Đô thị", "Lịch sử", "Quân sự", "Võng du", "Dị giới", "Huyền huyễn", "Khoa huyễn", "Ngôn tình", "Truyện teen", "Xuyên không", "Truyện ma", "Trinh thám"};

    public ListCategoryFragment() {

    }

    public ListCategoryFragment(Context c) {
        this.c = c;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_list_category, container, false);
    }

    @Override
    public void onStart() {
        super.onStart();
        int n = 14;
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(c, android.R.layout.simple_list_item_checked, categories);
        ListView listView = (ListView) getView().findViewById(R.id.lvCategory);
        listView.setAdapter(adapter);
        listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!isOnline()) {
                    Toast.makeText(getActivity().getBaseContext(), "Không có kết nối Internet!", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(c.getApplicationContext(), ListBookActivity.class);
                int cateID = 1;
                switch (position) {
                    case 0:
                        cateID = 1;
                        break;
                    case 1:
                        cateID = 2;
                        break;
                    case 2:
                        cateID = 4;
                        break;
                    case 3:
                        cateID = 6;
                        break;
                    case 4:
                        cateID = 8;
                        break;
                    case 5:
                        cateID = 9;
                        break;
                    case 6:
                        cateID = 10;
                        break;
                    case 7:
                        cateID = 11;
                        break;
                    case 8:
                        cateID = 12;
                        break;
                    case 9:
                        cateID = 13;
                        break;
                    case 10:
                        cateID = 14;
                        break;
                    case 11:
                        cateID = 18;
                        break;
                    case 12:
                        cateID = 19;
                        break;
                    case 13:
                        cateID = 20;
                        break;
                    default:
                        cateID = 1;
                        break;
                }
                intent.putExtra("cateID", cateID);
                intent.putExtra("readOnline", true);
                startActivity(intent);
            }
        });
    }

    /**
     * Kiem tra ket noi cua dien thoai
     *
     * @return
     */
    public boolean isOnline() {
        ConnectivityManager conMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo i = conMgr.getActiveNetworkInfo();
        if (i == null)
            return false;
        if (!i.isConnected())
            return false;
        if (!i.isAvailable())
            return false;
        return true;
    }
}
